﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ini编辑器CS
{
    public partial class Form2 : Form
    {
        const int Normal = 0;
        const int Flip = 1;
        Card card;
        string[] imgpath;
        //ListViewItem lvi;
        Form1 f1;

        public Form2(Form1 _f1, ListViewItem _lvi, Card _card)
        {
            f1 = _f1;
            //lvi = _lvi;
            imgpath = new string[2];
            card = _card;
            InitializeComponent();
            textBox1.Text = card.Name;
            textBox2.Text = card.Type;
            textBox3.Text = card.ID;
            if (textBox3.Text != "")
                textBox3.Enabled = false;
            imgpath[Normal] = card.img[Normal].path;
            imgpath[Flip] = card.img[Flip].path;
            if (imgpath[Normal] != null)
            {
                Image img = Image.FromFile(imgpath[Normal]);
                Image bmp = new Bitmap(img);
                img.Dispose();
                pictureBox1.Image = bmp;
            }
            if (imgpath[Flip] != null)
            {
                Image img = Image.FromFile(imgpath[Flip]);
                Image bmp = new Bitmap(img);
                img.Dispose();
                pictureBox2.Image = bmp;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
                return;
            for (int i = 0; i < Global.mod.cardPile.CardList.Count; i++)
            {
                if (Global.mod.cardPile.CardList[i].ID == textBox3.Text && Global.mod.cardPile.CardList[i] != card)
                {
                    MessageBox.Show("ID已存在！");
                    return;
                }
            }
            card.ID = textBox3.Text;
            card.Name = textBox1.Text;
            card.Type = textBox2.Text;
            if (card.path != null)
            {
                for(int i=0;i<f1.listView1.Items.Count;i++)
                {
                    if(f1.listView1.Items[i].SubItems[0].Text== card.ID)
                    {
                        f1.listView1.Items[i].SubItems[1].Text = card.Name;
                        f1.listView1.Items[i].SubItems[2].Text = card.Type;
                    }
                }
                card.img[Normal].path = imgpath[Normal];
                card.img[Flip].path = imgpath[Flip];
            }
            else
            {
                if(imgpath[Normal]==null||imgpath[Flip]==null)
                {
                    MessageBox.Show("请选择图片！");
                    return;
                }
                card.img = new DynamicImg[2];
                card.img[Normal] = new DynamicImg(imgpath[Normal]);
                card.img[Flip] = new DynamicImg(imgpath[Flip]);
                Global.mod.cardPile.CardList.Add(card);
                f1.AddCardItem(card);
                card.path = Global.mod.foldpath + card.ID + "\\";
            }
            Close();
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imgpath[Normal] = openFileDialog1.FileName;
                Image img = Image.FromFile(imgpath[Normal]);
                Image bmp = new Bitmap(img);
                img.Dispose();
                pictureBox1.Image = bmp;
            }
        }
        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imgpath[Flip] = openFileDialog1.FileName;
                Image img = Image.FromFile(imgpath[Flip]);
                Image bmp = new Bitmap(img);
                img.Dispose();
                pictureBox2.Image = bmp;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
